<?php

if(!defined('IN_DISCUZ')) {
	exit('Access Denied');
}

class table_common_smscode extends discuz_table
{
	public function __construct() {

		$this->_table = 'common_smscode';
		$this->_pk    = 'sid';

		parent::__construct();
	}
	public function get_sid_by_uid($sid) {
		$sid = intval($sid);
		$uid = 0;
		if($sid){
			$query = DB::fetch_first("SELECT sid ,uid FROM %t WHERE sid=%d AND uid >0", array($this->_table, $sid));
			if($query){
				$uid = $query['uid'];
			}
		}
		return $uid;
	}
	public function get_by_uid_modules($uid,$modules) {
		$uid = intval($uid);
		$modules = intval($modules);
		$data = array();
		if($uid && $modules){
			$data = DB::fetch_first("SELECT * FROM %t WHERE uid=%d AND modules=%s ORDER BY sid DESC LIMIT 0,1", array($this->_table, $uid, $modules));
		}
		return $data;
	}
	
	public function count_by_mobile($mobile,$date) {
		if(!$mobile) return 0;
		$sql = '';
		if($date){
			$date = dintval($date);
			if($date && $date >0){
				$sql = ' AND dateline >='.$date;
			}else{
				return 0;
			}
		}
		
		return DB::result_first("SELECT count(*) FROM %t WHERE mobile=%s $sql", array($this->_table, $mobile));
	}
	
	public function get_mobile_current_code($mobile) {
		if(!$mobile) return array();
		return DB::fetch_first("SELECT * FROM %t WHERE mobile=%s AND status=0 ORDER BY sid DESC LIMIT 0,1", array($this->_table, $mobile));
	}
	
	public function get_mobile_late($mobile,$status,$uid) {
		if(!$mobile) return array();
		$sql = '';
		if($status =='0' || $status){
			$status = intval($status);
			$sql .= " AND status='".$status."'";
		}
		if($uid) $sql .= " AND uid='".intval($uid)."'";
		return DB::fetch_first("SELECT * FROM %t WHERE mobile=%s $sql ORDER BY sid DESC LIMIT 0,1", array($this->_table, $mobile));
	}
	
	public function get_sid($sid,$mobile) {
		return DB::fetch_first("SELECT * FROM %t WHERE sid=%d AND mobile=%s", array($this->_table, $sid, $mobile));
	}
	
	public function get_mobile_late_uidstatus($mobile) {
		if(!$mobile) return array();
		return DB::fetch_first("SELECT * FROM %t WHERE mobile=%s AND uid>0 AND status=1 ORDER BY sid DESC LIMIT 0,1", array($this->_table, $mobile));
	}
	
	public function fetch($sql,$start,$perpage) {
		$start = intval($start);
		$perpage = intval($perpage);
		if($sql){
			$sql = trim($sql);
		}
		return DB::fetch_all("SELECT * FROM %t WHERE 1 $sql ORDER BY sid DESC LIMIT $start,$perpage", array($this->_table));
	}
	public function fetch_count($sql) {
		$start = intval($start);
		$perpage = intval($perpage);
		if($sql){
			$sql = trim($sql);
		}
		return DB::result_first("SELECT count(*) FROM %t WHERE 1 $sql", array($this->_table));
	}
}

?>